package com.yrmao.yrmtimedialog.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yrmao.yrmtimedialog.R;
import com.yrmao.yrmtimedialog.ui.models.MTimeDataBean;
import com.yrmao.yrmtimedialog.ui.widget.MTimeDialog;
import com.yrmao.yrmtimedialog.utils.DisplayUtil;

import java.util.List;

import static com.yrmao.yrmtimedialog.ui.widget.MTimeDialog.typeTime;


/**
 * Created by yemao on 16/11/25.
 */

public class MTimeAdapter extends RecyclerView.Adapter {
    int count = 0;
    private List<MTimeDataBean> list;
    private Context context;
    private OnTimeItemListener mOnTimeItemListener;
    private View contview;

    public MTimeAdapter(List<MTimeDataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        contview = LayoutInflater.from(context).inflate(R.layout.itme_dialog_time, null);
        return new MViewHolder(contview);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MViewHolder mholder = (MViewHolder) holder;
        setHeight(mholder.mtv_time);
        setTypeText(mholder, position);
        mholder.mtv_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MTimeDialog.typeTime == MTimeDialog.TIME_DAY) {
                    if ((position + 1) > count && list.size() + count > position)
                        onTimeOnclick(position - count, list.get(position - count).time);
                } else {
                    onTimeOnclick(position, list.get(position).time);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        if (MTimeDialog.typeTime == MTimeDialog.TIME_DAY) {

            count = 7 + list.get(0).week;

            int suncount = list.size() + count;
            if (suncount <= 42)
                suncount = 42 + 1;
            return list == null ? 0 : suncount;

        } else if (typeTime == MTimeDialog.TIME_MONTH) {

            count = 0;
            return list == null ? 0 : list.size();

        }

        return 0;

    }

    public void setHeight(TextView mtext) {
        //设置item的高度=获取屏幕的宽度*0.85-magin20
        int dialogWidth = (int) ((double) this.context.getResources().getDisplayMetrics().widthPixels * 0.85D) - DisplayUtil.dip2px(context, 20);

        if (MTimeDialog.typeTime == MTimeDialog.TIME_MONTH) {
            ViewGroup.LayoutParams lp;
            lp = mtext.getLayoutParams();
            lp.height = dialogWidth / 6;
            mtext.setLayoutParams(lp);
        } else if (MTimeDialog.typeTime == MTimeDialog.TIME_DAY) {
            ViewGroup.LayoutParams lp;
            lp = mtext.getLayoutParams();
            lp.height = dialogWidth / 7;
            mtext.setLayoutParams(lp);
        }

    }


    private class MViewHolder extends RecyclerView.ViewHolder {
        private TextView mtv_time;

        public MViewHolder(View itemView) {
            super(itemView);
            mtv_time = (TextView) itemView.findViewById(R.id.tv_item_dialog_time);
        }
    }

    private void setTypeText(MViewHolder mholder, int position) {
        if (MTimeDialog.typeTime == MTimeDialog.TIME_DAY) {
            if (position + 1 <= count) {
                mholder.mtv_time.setBackgroundResource(R.color.white);
                mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.text_color_hint));

            } else {
                mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.black));

            }

            switch (position) {
                case 0:
                    mholder.mtv_time.setText("日");
                    break;
                case 1:
                    mholder.mtv_time.setText("一");
                    break;
                case 2:
                    mholder.mtv_time.setText("二");
                    break;
                case 3:
                    mholder.mtv_time.setText("三");
                    break;
                case 4:
                    mholder.mtv_time.setText("四");
                    break;
                case 5:
                    mholder.mtv_time.setText("五");
                    break;
                case 6:
                    mholder.mtv_time.setText("六");
                    break;
                default:
                    if (position < 7 + list.get(0).week) {
                        mholder.mtv_time.setText("");
                        return;
                    }
                    if (list.size() + count - 1 < position) {
                        mholder.mtv_time.setText("");
                        mholder.mtv_time.setBackgroundResource(R.color.white);
                        return;
                    }
                    int dposition = position - count;

                    mholder.mtv_time.setText(list.get(dposition).time + "");

                    if (list.get(dposition).isAttime && list.get(dposition).isCheackTime) {

                        mholder.mtv_time.setBackgroundResource(R.drawable.round_blue);
                        mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.white));

                    } else if (!list.get(dposition).isAttime && list.get(dposition).isCheackTime) {

                        mholder.mtv_time.setBackgroundResource(R.drawable.round_blue);
                        mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.white));

                    } else if (list.get(dposition).isAttime && !list.get(dposition).isCheackTime) {

                        mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.ammeter_blue));

                    } else {

                        mholder.mtv_time.setBackgroundResource(R.drawable.teacher_actionbar_btn_selecter_rdio);
                        mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.black));

                    }

            }
        } else if (MTimeDialog.typeTime == MTimeDialog.TIME_MONTH) {

            mholder.mtv_time.setText(list.get(position).time + "月");

            if (list.get(position).isAttime && list.get(position).isCheackTime) {

                mholder.mtv_time.setBackgroundResource(R.drawable.round_blue);
                mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.white));

            } else if (!list.get(position).isAttime && list.get(position).isCheackTime) {

                mholder.mtv_time.setBackgroundResource(R.drawable.round_blue);
                mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.white));

            } else if (list.get(position).isAttime && !list.get(position).isCheackTime) {

                mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.ammeter_blue));

            } else {

                mholder.mtv_time.setBackgroundResource(R.drawable.teacher_actionbar_btn_selecter_rdio);
                mholder.mtv_time.setTextColor(context.getResources().getColor(R.color.black));

            }
        }

    }

    private void onTimeOnclick(int position, int time) {
        if (mOnTimeItemListener != null)
            mOnTimeItemListener.cheackItem(position, time);
    }

    public void setOnTimeItemListener(OnTimeItemListener listener) {
        this.mOnTimeItemListener = listener;
    }

    public interface OnTimeItemListener {
        void cheackItem(int position, int time);
    }
}
